<?php
/**
 *
 * PHP API 联系（发email，发站内信）控制器
 * @author jeff liu
 * powered by jeff liu
 */
class ContactApp extends ApiApp
{

    public function __construct()
    {
        $this->ContactApp();
    }

    public function ContactApp()
    {
        parent::__construct();
    }

    /**
     * 发送邮件
     * @author jeff liu
     * @tpl_name required 使用email模版的名称
     * @id string required 需要发送邮件的用户id 多个id之间使用","分割
     *
     * 需要的其他自定义参数 可以使用POST,GET的方式传递过来
     * 在模版中需要一个inovte_no 的参数, ....&inovte_no=3&......
     * powered by jeff liu 2011-07-14
     */
    public function apiSendMailToUser()
    {
        $result = array();
        $data = array();
        $data = array_merge($data, $_GET, $_POST);

        $mail_info = get_mail($data['tpl_name'], $data);
        $mail_rs = $this->_mailto(urldecode($data['email']), $mail_info['subject'], $mail_info['message']);
        $result[] = array('mail_rs' => $mail_rs, 'id' => $data['user_id']);
        $this->echo_result($result);

    }

    /**
     * 发送邮件
     * @TODO 需要完善
     */
    public function apiSendMailToUserWithoutTpl()
    {
        $data = $this->_get_required_data();
        $result = array();
        foreach ($data['member_info'] as $member) {
            $mail_rs = $this->_mailto($data['user_info']['email'], $mail_info['subject'], $mail_info['message']);
            $result[] = array('mail_rs' => $mail_rs, 'id' => $member['user_id']);
        }
        $this->echo_result($result);

    }

    /**
     *
     * @TODO 需要完善
     */
    public function apiSendMailToAnonymous()
    {

    }

    /**
     * @TODO 需要完善
     */
    public function apiSendMailToAnonymousWithoutTpl()
    {

    }

    /**
     * 发送站内信
     * @author jeff liu
     * @id string required 需要发送站内信的user_id
     * @tpl_name string required 使用的站内信的模版
     * 需要的其他自定义参数 可以使用POST,GET的方式传递过来
     * 在模版中需要一个inovte_no 的参数, ....&inovte_no=3&......
     * powered by jeff liu 2011-07-14
     */
    public function apiSendMessage()
    {
        //$data = $this->_get_required_data(false, false);
        $ms =& ms();
        $result = array();
        $data = array();
        $data = array_merge($data, $_GET, $_POST);
        $content = get_msg($data['tpl_name'], $data);
        $msg_id = $ms->pm->send(MSG_SYSTEM, $data['user_id'], '', $content);
        $result = array('msg_id' => $msg_id, 'id' => $data['user_id']);
        $this->echo_result($result);
    }

    /**
     * 发送短信息
     * @author jeff liu
     * powered by jeff liu 2011-07-14
     */
    public function apiSendPM()
    {

    }

    /**
     *
     * @param boolean $is_get_user_info 是否获得用户信息
     * @param boolean $is_tpl_required 是否必须模版
     * @return type
     */
    private function _get_required_data($is_get_user_info = true, $is_tpl_required = true)
    {
        $user_id = $_REQUEST['id'];
        $tpl_name = $_REQUEST['tpl_name'];

        $user_ids = array();
        if (empty($user_id) && $is_get_user_info) {
            ApiError::showError('user_id_required', 1);
            exit;
        }
        if (empty($tpl_name) && $is_tpl_required) {
            ApiError::showError('tpl_name_required', 1);
            exit;
        }
        $tmp_ids = explode(',', $user_id);
        foreach ((array)$tmp_ids as $id) {
            if (!empty($id)) {
                $user_ids[] = $id;
            }
        }
        $member_info = array();
        if ($is_get_user_info) {
            $member_mod =& api_model('member');
            $sql = sprintf(
                'SELECT user_id, user_name, email, phone_mob
                            FROM %s
                            WHERE user_id IN(%s)',
                $member_mod->table,
                implode(',', $user_ids)
            );
            $member_info = $member_mod->getAll($sql);
        }


        return array(
            'member_info' => $member_info,
            'tpl_name' => $tpl_name,
        );
    }

}